package org.huqiwen.blog.logic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.huqiwen.blog.dao.DataSource;
import org.huqiwen.blog.dao.ExecuteOptionSQL;
import org.huqiwen.blog.data.LinkDataBean;
/**
 * 处理友情链接的相关业务逻辑
 * @author Administrator
 *
 */
public class LinkLogicBean {
	/**
	 * 返回友情链接列表
	 * @return
	 */
	public List<LinkDataBean> findAll(){
		List<LinkDataBean> list = new ArrayList<LinkDataBean>();
		Statement stmt = null;
		ResultSet rs = null;
		try{
			String sql = "select * from links";
			stmt = DataSource.getConnection().createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				LinkDataBean bean = new LinkDataBean();
				bean.setLinkId(rs.getString(1));
				bean.setLinkName(rs.getString(2));
				bean.setLinkUrl(rs.getString(3));
				bean.setLinkImage(rs.getString(4));
				bean.setLinkDescription(rs.getString(5));
				bean.setLinkDate(rs.getString(6));
				bean.setLinkUpdate(rs.getString(7));
				list.add(bean);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null) rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} 
		}
		return list;
	}
	/**
	 * 根据传入的参数，添加友情链接
	 * @param linkName
	 * @param linkUrl
	 * @param linkDes
	 * @return
	 */
	public int addLink(String linkName,String linkUrl,String linkDes){
		return ExecuteOptionSQL.UpdateSQL("insert into links(link_name,link_url,link_description,link_date,link_update) values('" +
				linkName+"','"+linkUrl+"','"+linkDes+"',now(),now())");
	}
	/**
	 * 根据传入的参数，友情链接的ID,删除相应的友情链接
	 * @param id
	 * @return
	 */
	public int delLink(String id){
		return ExecuteOptionSQL.UpdateSQL("delete from links where link_id = "+id);
	}
	/**
	 * 根据传入的参数，友情链接的id，查找相应的友情链接，并返回链接实体类
	 * @param id
	 * @return
	 */
	public LinkDataBean findLinkById(String id){
		LinkDataBean bean = new LinkDataBean();
		Statement stmt = null;
		ResultSet rs = null;
		try{
			stmt = DataSource.getConnection().createStatement();
			rs = stmt.executeQuery("select * from links where link_id =" +id);
			while(rs.next()){
				bean.setLinkId(rs.getString(1));
				bean.setLinkName(rs.getString(2));
				bean.setLinkUrl(rs.getString(3));
				bean.setLinkDescription(rs.getString(5));
				bean.setLinkImage(rs.getString(4));
				bean.setLinkDate(rs.getString(6));
				bean.setLinkUpdate(rs.getString(7));
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(stmt!=null) stmt.close();
				if(rs!=null) rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return bean;
	}
	/**
	 * 根据传入的参数，对友情连接进行更新
	 * @param id
	 * @param linkName
	 * @param linkUrl
	 * @param linkDes
	 * @return
	 */
	public int updateLink(String id,String linkName,String linkUrl,String linkDes){
		return ExecuteOptionSQL.UpdateSQL("update links set link_name='"+linkName
				+"',link_url = '"+linkUrl+"',link_description = '"+linkDes
				+"',link_update=now()");
	}
}
